package org.example.shoppingmall.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.math.BigDecimal;

public class ResponseUtil {
    public static void genBaseResponse(HttpServletResponse resp, int code, String msg, Object data) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        jsonObject.put("data",data);
        resp.getWriter().write(JSON.toJSONString(jsonObject));
    }
    public static void gen404Response(HttpServletResponse resp, String msg) throws IOException {
        genBaseResponse(resp, 404, msg, null);
    }
    public static void gen500Response(HttpServletResponse resp, String msg) throws IOException {
        genBaseResponse(resp, 500, msg, null);
    }
    public static void genSuccessResponse(HttpServletResponse resp, String msg, Object data) throws IOException {
        genBaseResponse(resp, 0, msg, data);
    }

    /**
     * 分页并返回单项总数
     * @param resp
     * @param msg
     * @param total
     * @param data
     * @throws IOException
     */
    public static void genCountAndListResponce(HttpServletResponse resp, String msg,Integer total, Object data) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg",msg);
        jsonObject.put("total",total);
        jsonObject.put("data",data);
        resp.getWriter().write(JSON.toJSONString(jsonObject));
    }

    /**
     * 扣款成功并生成订单
     * @param resp
     * @param msg
     * @param s
     * @param data
     */
    public static void genSettlement(HttpServletResponse resp, String msg, String s, Object data) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg",msg);
        jsonObject.put("remainder",s);
        jsonObject.put("data",data);
        resp.getWriter().write(JSON.toJSONString(jsonObject));
    }
}
